/**
 * 
 */
package com.audi.box;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

import com.audi.box.server.Server;
import com.audi.box.server.ServerListenerCallBack;
import com.audi.utils.DBHelper;

/**
 * @author HuyPVA
 * 
 */
public class BoxService extends Service implements ServerListenerCallBack {
	public static String ACTION = "action";
	private static int ACTION_SETUP = 1;
	private static int ACTION_START = 2;
	private static int ACTION_STOP = 3;

	private Server server;
	private static int TCP_PORT = 7777;
	private String id = "pvanhhuy@gmail.com";
	private String pw = "pvanhhuy";

	private final IBinder mBinder = new LocalBinder();

	public class LocalBinder extends Binder {
		BoxService getService() {
			return BoxService.this;
		}
	}

	public BoxService() {
		Log.d("AudiBox", "[BoxService.java] New");
	}

	@Override
	public IBinder onBind(Intent intent) {
		return mBinder;
	}

	@Override
	public void onCreate() {
		Log.d("AudiBox", "[BoxService.java] Created");
		Toast.makeText(getApplicationContext(), "Service Created", 1).show();
		Intent intent = new Intent();
		intent.putExtra(ACTION, ACTION_START);
		changeServerStatus(intent);
		super.onCreate();
	}

	@Override
	public void onDestroy() {
		Log.d("AudiBox", "[BoxService.java] Destroy");
		Toast.makeText(getApplicationContext(), "Service Destroy", 1).show();
		super.onDestroy();
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		Log.d("Audibox", "[BoxService.java] Working");
		Toast.makeText(getApplicationContext(), "Service Working", 1).show();
		return super.onStartCommand(intent, flags, startId);
	}

	private void changeServerStatus(Intent intent) {
		Bundle bundle = intent.getExtras();
		if (bundle != null) {
			int action = bundle.getInt(ACTION);
			if (action == ACTION_SETUP) {
				Log.d("AudiBox", "[BoxService.java] Setup server");
				id = bundle.getString("id");
				pw = bundle.getString("pw");
			} else if (action == ACTION_START) {
				Log.d("AudiBox", "[BoxService.java] Start server");
				server = new Server(this, TCP_PORT, id, pw, this);
				server.running();
			} else if (action == ACTION_STOP) {
				server.stop();
			} else {
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.asus.audi.box.server.ServerListenerCallBack#initCallback(boolean)
	 */
	@Override
	public void startCallback(boolean bValue) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.asus.audi.box.server.ServerListenerCallBack#stopCallback(boolean)
	 */
	@Override
	public void stopCallback(boolean bValue) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.asus.audi.box.server.ServerListenerCallBack#processCallback(java.
	 * lang.String, java.lang.String)
	 */
	@Override
	public void processCallback(String tag, String data) {
		// TODO Auto-generated method stub

	}

}
